<?php

//TODO think to remove this file and class

class field
{
    public $spanField;
    public $spanLabel;
    public $field;

    function field($id, $field, $label, $labelWidth, $inputWidth, $height, $left, $top)
    {
        $this->spanLabel = new span("label$id", $label, 'label');
        $this->spanLabel ->setBounds($left, $top, $labelWidth, $height);
        $this->field     = $field;
        $this->spanField = new span("span$id", $this->field, 'field');
        $this->spanField ->setBounds($left+$labelWidth, $top ,$inputWidth, $height);
    }

    function __toString()
    {
        $result =       $this->spanField->__toString();
        $result.=       $this->spanLabel->__toString();

        return $result;
    }

    function getField()
    {
        return $this->field;
    }

    function getSpanField()
    {
        return $this->spanField;
    }

    function getSpanLabel()
    {
        return $this->spanLabel;
    }

    function setValue($value)
    {
        $this->getField()->setValue($value);
    }

    function getValue()
    {
        return $this->getField()->getValue();
    }

}


class textfield extends field
{
    function textfield($id, $type='text', $label, $labelWidth, $inputWidth, $height, $left, $top, $class=NULL)
    {
        $field =  new input($id, $type, $class);
        $field -> setDimensions($inputWidth, NULL );
        parent::field($id, $field, $label, $labelWidth, $inputWidth, $height, $left, $top);
    }
}

/**
 * Create a Select with label
 */
class selectfield extends field
{
    public function selectField($id, $options, $label, $labelWidth, $inputWidth, $height, $left, $top, $class=NULL)
    {
        $field = new select($id, 1, NULL, $options, $class);
        $field -> setDimensions($inputWidth, NULL );
        parent::field($id, $field, $label, $labelWidth, $inputWidth, $height, $left, $top);
    }

}


/**
 *  Create a textArea with label
 */
class textAreaField extends field
{
	public function textAreaField($id, $rows, $label, $labelWidth, $inputWidth, $height, $left, $top, $class=NULL)
	{
		$field = new textarea($id, $rows, 10, '', $class);
		$field -> setDimensions($inputWidth, NULL );
        parent::field($id, $field, $label, $labelWidth, $inputWidth, $height, $left, $top);
	}
}


    /**
    * Creates a div that has mouse move element.
    * But onmousedown event is ocupied.
    * Need genericDrag.js
    * Please do not include input inside it, the inputs will not work as espected.
    * To do this use DivForm
    *
    */
    class divMove extends div
    {
        function divMove($id, $inner=NULL , $class=NULL)
        {
            parent::div($id, $inner, $class);
            $this->addEvent('onmousedown', "dragStart(event, '$id')" );
        }

    }


/**
    * Creates a div that has mouse move element.
    * But onmousedown event is ocupied.
    * Need genericDrag.js
    *
    */
    class divForm extends div
    {
        function divForm($id, $inner=NULL, $width=null, $height=null, $title=NULL, $class=NULL, $classTitle = null, $position = 'absolute' )
        {
            parent::div($id, NULL, $class);

            if ($height || $width || $position)
            {
                $this->setBounds(null,null, $width, $height, $position);
            }

            $this->inner['bar'] = new div("bar$id", array(new Span('',$title)) );
            $this->inner['bar']->onmousedown = "dragStart(event, '$id')";
            $this->inner['bar']->style->width  = '100%';
            $this->inner['bar']->style->height = '20px';
            $this->inner['bar']->class = $classTitle;
            $button = new Button($id.'hide','X', "document.getElementById('$id').style.display='none'", 'close-button');
            $button  ->setStyleArray('margin-top: 2px ; margin-right: 2px; width:16px; height:16px; font-size:10px;');
            $this->inner['bar']->inner['button']=$button;
            $this->addInner($inner);
        }

        function getBar()
        {
            return $this->inner['bar'];
        }

        function setCloseOperation($script)
        {
            $this->inner['bar']->inner['button']->addEvent('onclick', $script);
        }

	//TODO rest of getters and setters

   }


    /**
    * create a form with ajax suport
    * It add prototype automatically
    * It need a global variable called $page
    */
    class ajaxForm extends Form
    {
        function ajaxForm($id=NULL, $inner=NULL, $action='index.php', $method='post', $onSubmit=NULL, $class=NULL)
        {
            parent::form($id, $inner, $action, $method, $onSubmit, $class);
            global $page;
            if ( $id && $action && $method)
            {
	            $page->addJsCode('
	            function ajaxCall'.$this->id.'(returnElement, phpFunction)
	            {
	                var data                = $(\''.$this->id.'\').serialize(true);
	                data[\'ajax\']          = true;
	                data[\'phpFunction\']   = phpFunction;
	                new Ajax.Updater(returnElement, \''.$this->action.'\', {method: \''.$this->method.'\',  parameters: data, evalScripts: true} );
	            }
                ');
            }
        }
    }

class imageLink extends link
{
    public function imagelink($id=NULL, $src=NULL, $href=NULL, $class=NULL, $label=NULL)
    {
        $img = new img('img'.$id, $src);
        $img->border = '00';
        parent::link($id, $img, $href, $class, $label);
    }

}

?>